package cn.org.atool.generator;

import cn.org.atool.fluent.mybatis.metadata.DbType;
import cn.org.atool.generator.annotation.*;
import cn.org.atool.generator.database.model.CustomizedColumn;
import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.test4j.module.database.proxy.DataSourceCreator;

import javax.sql.DataSource;
import java.util.List;

/**
 * DataMapGeneratorTest
 *
 * @author darui.wu 2020/5/7 11:47 上午
 */
public class GeneratorMain {
    private final static String url = "jdbc:mysql://localhost:3306/fluent_mybatis?useUnicode=true&characterEncoding=utf8&useInformationSchema=true";

    public static DataSource newDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl(url);
        dataSource.setUsername("root");
        dataSource.setPassword("password");
        return dataSource;
    }

    @Test
    @Disabled
    public void generate() {
        DataSourceCreator.create("dataSource");
        new FileGenerator()
            .dataSource(newDataSource())
            //.column("student", "address", c -> c.setJavaType(List.class, String.class))
            .column((table, column) -> new CustomizedColumn(column))
            .useJSR310Types()
            .with(Empty1.class, Empty2.class)
            .generate();
    }

    @Tables(
        dbType = DbType.MYSQL,
        schema = "fluent_mybatis",
        srcDir = "src/main/java",
        testDir = "src/test/java",
        daoDir = "target/dao",
        basePack = "cn.org.atool.generator.demo",
        tables = {
            @Table(value = "home_address:MyAddressPoJo"),
            @Table(value = "student", tablePrefix = "t_",
                gmtCreated = "gmt_created", gmtModified = "gmt_modified",
                columns = @Column(value = "address", javaType = {List.class, String.class}))
        },
        relations = {
            @Relation(source = "student", target = "home_address", type = RelationType.TwoWay_N_1,
                where = "home_address_id=id && env=env && is_deleted=is_deleted"),
            @Relation(method = "findAnother", source = "student", target = "home_address", type = RelationType.OneWay_0_1, cache = false)
        },
        entitySuffix = "PoJo", logicDeleted = "is_deleted"
    )
    static class Empty1 {
    }

    @Tables(
        url = url, username = "root", password = "password",
        schema = "fluent_mybatis",
        srcDir = "src/main/java",
        testDir = "src/test/java",
        daoDir = "target/dao",
        basePack = "cn.org.atool.generator.demo",
        tables = {
            @Table(value = {"no_auto_id"}, mapperPrefix = "new"),
            @Table(value = {"no_primary"}, useDao = false)},
        version = "lock_version",
        isLombok = false, isSetterChain = false, isRichEntity = false
    )
    static class Empty2 {
    }
}